package socketmvc.server.processor;

import socketmvc.core.packets.PacketStaticFactory;
import socketmvc.core.packets.basic.IPacket;
import socketmvc.core.packets.basic.JsonBasicPacket;
import socketmvc.core.packets.biz.SockFile;
import socketmvc.core.packets.biz.SockMultiFile;
import socketmvc.server.context.SocketChannel;
import socketmvc.server.context.SocketChannelThreadWrapper;
import java.io.File;

public interface IBaseProcessor {

    SocketChannel iContext = new SocketChannelThreadWrapper();


    default void responsePacket(IPacket response) {
        SocketChannel context = ((SocketChannelThreadWrapper) iContext).getContext();
        IPacket packet = context.getPacket();
        if (packet != null){
            response.setResponseKey(packet.getResponseKey());
            response.setResponsePkt(true);
        }
        context.send(response);
    }

    default void response(Object simpleParam) {
        JsonBasicPacket response = PacketStaticFactory.createSimplePacket(simpleParam);
        responsePacket(response);
    }

    default void sendToUser(IPacket packet,String user) {
        iContext.sendToUser(user, (packet));
    }

    default void sendToUser(String user, Object simpleParam) {
        iContext.sendToUser(user, (PacketStaticFactory.createSimplePacket(simpleParam)));
    }

    default void sendToUser(String user, Object simpleParam, File file) {
        iContext.sendToUser(user, (PacketStaticFactory.createBinPacket(simpleParam, file)));
    }

    default void sendToUser(String user, Object simpleParam, SockFile file) {
        iContext.sendToUser(user, (PacketStaticFactory.createBinPacket(simpleParam, file)));
    }

    default void sendToUser(String user, Object simpleParam, SockMultiFile file) {
        iContext.sendToUser(user, (PacketStaticFactory.createBinPacket(simpleParam, file)));
    }

    default void sendToGroup(IPacket packet,String group) {
        iContext.sendToGroup(group, (packet));
    }

    default void sendToGroup(String group, Object simpleParam) {
        iContext.sendToGroup(group, (PacketStaticFactory.createSimplePacket(simpleParam)));
    }

    default void sendToGroup(String group, Object simpleParam, File file) {
        iContext.sendToGroup(group, (PacketStaticFactory.createBinPacket(simpleParam, file)));
    }

    default void sendToGroup(String group, Object simpleParam, SockFile file) {
        iContext.sendToGroup(group, (PacketStaticFactory.createBinPacket(simpleParam, file)));
    }

    default void sendToGroup(String group, Object simpleParam, SockMultiFile file) {
        iContext.sendToGroup(group, (PacketStaticFactory.createBinPacket(simpleParam, file)));
    }
}
